<?php

namespace app\modules\web\controllers;

use app\common\services\ConstantMapServices;
use app\common\services\UrlServices;
use app\models\log\AppAccessLog;
use app\models\User;
use app\modules\web\controllers\common\BaseController;


class AccountController extends BaseController
{
    public function __construct($id, $module, array $config = [])
    {
        parent::__construct($id, $module, $config);
        //设置布局文件为加载当前模块下的layouts文件夹下的main.php布局文件
        $this->layout="main";
    }

    //账户列表
    public function actionIndex()
    {
        $status=intval($this->get("status",ConstantMapServices::$status_default));
        $mix_kw=trim($this->get("mix_kw",""));

        //指定分页的默认页码
        $p=intval($this->get('p',1));

        $query=User::find();

        if($status > ConstantMapServices::$status_default){
            $query->andWhere(["status"=>$status]);
        }

        if($mix_kw){
            //如果不加false时 执行SQL语句会加上两个% 执行就会有误
            $where_nickname=['like','nickname','%'.$mix_kw.'%',false];
            $where_mobile=['like','mobile','%'.$mix_kw.'%',false];
            $query->andWhere(["or",$where_nickname,$where_mobile]);
        }

        //分页功能 需要两个参数 1、符合条件的总记录数量 2、每页展示的数量
        //每页显示的数量
        $page_size=50;
        //符合条件的总记录数量
        $total_res_count=$query->count();
        //总页数
        $total_page=ceil($total_res_count/$page_size);

        $lists=$query->orderBy(["uid"=>SORT_DESC])
            ->offset(($p-1)*$page_size)
            ->limit($page_size)
            ->all();
        return $this->render('index',
            [
                'lists'=>$lists,
                'status_mapping'=>ConstantMapServices::$status_mapping,
                'search_condition'=>[
                    'mix_kw'=>$mix_kw,
                    'status'=>$status,
                    'p'=>$p
                ],
                'pages'=>[
                    'total_count'=>$total_res_count,
                    'page_size'=>$page_size,
                    'total_page'=>$total_page,
                     'p'=>$p
                ],

            ]
        );
    }

    //账户编辑或添加
    public function actionSet()
    {
        if(\Yii::$app->request->isGet){
            $id=intval($this->get("id",0));
            $info=[];
            if($id){
                $info=User::find()->where(['uid'=>$id])->one();
            }
            return $this->render('set',['info'=>$info]);
        }

        $id=intval($this->post("id",0));
        $nickname=trim($this->post("nickname",""));
        $mobile=trim($this->post("mobile",""));
        $email=trim($this->post("email",""));
        $login_name=trim($this->post("login_name",""));
        $login_pwd=trim($this->post("login_pwd",""));

        $data_now=date("Y-m-d H:i:s");
        if(mb_strlen($nickname,"utf-8")<1){
            return $this->renderJson([],"请输入用户名",-1);
        }

        if(mb_strlen($mobile,"utf-8")<1){
            return $this->renderJson([],"请输入手机号",-1);
        }

        if(mb_strlen($email,"utf-8")<1){
            return $this->renderJson([],"请输入邮箱",-1);
        }

        if(mb_strlen($login_name,"utf-8")<1){
            return $this->renderJson([],"请输入登录名",-1);
        }

        if(mb_strlen($login_pwd,"utf-8")<1){
            return $this->renderJson([],"请输入登录密码",-1);
        }

        $has_in=User::find()->where(['login_name'=>$login_name])->andWhere(['!=','uid',$id])->count();
        if($has_in){
            return $this->renderJson([],"该用户名已存在,请换一个试试~~",-1);
        }

        $info=User::find()->where(['uid'=>$id])->one();
        if($info){//编辑
            $model_user=$info;
        }else{//添加
            $model_user=new User();
            $model_user->setSalt();
            $model_user->created_time=$data_now;
        }

        $model_user->nickname=$nickname;
        $model_user->mobile=$mobile;
        $model_user->email=$email;
        $model_user->avatar=ConstantMapServices::$default_avatar;
        $model_user->login_name=$login_name;
        if($login_pwd != ConstantMapServices::$default_password){
            $model_user->setPassword($login_pwd);
        }

        $model_user->updated_time=$data_now;

        $model_user->save(0);
        return $this->renderJson([],"操作成功~~",200);

    }

    //账户详情
    public function actionInfo()
    {
        $id=intval($this->get('id',0));

        $reback_url=UrlServices::buildWebUrl('/account/index');
        if(!$id){
            return $this->redirect($reback_url);
        }

        $info=User::find()->where(['uid'=>$id])->one();
        if(!$info){
            return $this->redirect($reback_url);
        }

        $access_lists=AppAccessLog::find()->where(['uid'=>$info['uid']])->orderBy(['id'=>SORT_DESC])->limit(10)->all();
        return $this->render('info',['info'=>$info,'access_lists'=>$access_lists]);
    }

    //操作方法
    public function actionOps()
    {
        if(!\Yii::$app->request->isPost){
            return $this->renderJson([],'系统繁忙~',-1);
        }

        $uid=intval($this->post("uid",0));

        $act=trim($this->post("act",''));

        if(!$uid){
            return $this->renderJson([],'请选择要操作的账号~',-1);
        }

        if(!in_array($act,["remove","recover"]))
        {
            return $this->renderJson([],'操作有误请重试~',-1);
        }

        $user_info=User::find()->where(['uid'=>$uid])->one();

        if(!$user_info){
            return $this->renderJson([],'指定账号不存在~',-1);
        }

        switch($act){
            case "remove":
                $user_info->status=0;
                break;
            case "recover":
                $user_info->status=1;
                break;
        }
        $user_info->updated_time=date("Y-m-d H:i:s");
        $user_info->update(0);
        return $this->renderJson([],'操作成功',200);
    }
}
